package com.loyal.loyalerp.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageInfo;
import com.loyal.loyalerp.commons.ServerResponse;
import com.loyal.loyalerp.model.Employee;
import com.loyal.loyalerp.model.vo.GridVO;
import com.loyal.loyalerp.service.EmployeeService;

@Controller
public class UserController{
	@Autowired
	EmployeeService userService;

	@RequestMapping("/editUser") // 增删改
	@ResponseBody
	public Object editUser(String oper, @Valid Employee user,BindingResult result,HttpServletResponse response) throws IOException {
		
		if(result.hasErrors()){
			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
			return ServerResponse.createByError("参数验证失败",result.getAllErrors());
		}
		Boolean bool = userService.editEmployee(oper, user);
		if (bool) {
			return ServerResponse.createBySuccess();
		}
		else {
//			response.setStatus(HttpServletResponse.SC_FORBIDDEN);
			return ServerResponse.createByError();
		}
		
		
	}

	@RequestMapping("/listUser")
	@ResponseBody

	public Object listUser(String _search, Integer rows, Integer page, String sord,String searchField, String searchString) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
		if ("false".equals(_search) || StringUtils.isEmpty(_search))// 列表显示
		{
			PageInfo<Employee> pageInfo = userService.listEmployee(page, rows, sord);
			
			return GridVO.fromPageInfo(pageInfo);
		} 
		//模糊查询
		else {
			PageInfo<Employee> pageInfo = userService.findEmployee(page, rows, sord, searchField, searchString);
			return GridVO.fromPageInfo(pageInfo);
		}
			

	}

}
